package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.xuexiang.xui.widget.edittext.ClearEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/7/27
 * @description
 **/
public class PaymentApplyConfirmActivity extends BaseActivity {
    @BindView(R.id.activity_payment_confirm_codeEditText)
    ClearEditText codeEditText;

    private String mOrderNo;
    private String mAmount;

    private UserInfoModel mUserInfoModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_payment_confirm;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "提现确认";
    }

    @Override
    public void init() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderNo = bundle.getString(KeyValue.KEY_ORDER_NO);
            mAmount = bundle.getString(KeyValue.KEY_AMOUNT);
        }
        mUserInfoModel = new UserInfoModel();

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_payment_confirm_confirmButton)
    public void confirm() {
        String code = codeEditText.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }

        if (code.length() < 6) {
            showToast("验证码不能少于6位");
            return;
        }
        requestPaymentConfirm(code);

    }

    private void requestPaymentConfirm(String code) {
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", mOrderNo);
        map.put("smsCode", code);
        mUserInfoModel.paymentConfirmModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
                    @Override
                    public void onNext(CommonResponse baseResponse) {
                        if (baseResponse.getCode() == 20000) {
                            Bundle bundle = new Bundle();
                            bundle.putString(KeyValue.KEY_AMOUNT, mAmount);
                            openActivity(PaymentSuccessActivity.class, bundle);
                        } else {
                            showToast(baseResponse.getMsg());
                        }

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        showToast(e.message);
                    }
                }
        );
    }
}
